package com.lwj.utils;

import org.apache.tika.metadata.HttpHeaders;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.helpers.DefaultHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TiKaToolKit {

    /**
     * 获取类型
     *
     * @param file
     */
    public static String getMimeType(File file) {
        if (file.isDirectory()) {
            return "the target is a directory";
        }
        AutoDetectParser parser = new AutoDetectParser();
        parser.setParsers(new HashMap<>());
        Metadata metadata = new Metadata();
        metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, file.getName());
        try (InputStream stream = new FileInputStream(file)) {
            parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return metadata.get(HttpHeaders.CONTENT_TYPE);
    }
    public static String getMimeType(InputStream stream,String fileName) {
        try {
            AutoDetectParser parser = new AutoDetectParser();
            parser.setParsers(new HashMap<>());
            Metadata metadata = new Metadata();
            metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, fileName);
            parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
            return metadata.get(HttpHeaders.CONTENT_TYPE);
        }catch (Exception e){
            throw new RuntimeException();
        }
    }

    public static String getMimeType( MultipartFile file) {
        try {
            AutoDetectParser parser = new AutoDetectParser();
            parser.setParsers(new HashMap<>());
            Metadata metadata = new Metadata();
            metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, file.getOriginalFilename());
            parser.parse(file.getInputStream(), new DefaultHandler(), metadata, new ParseContext());
            return metadata.get(HttpHeaders.CONTENT_TYPE);
        }catch (Exception e){
            throw new RuntimeException();
        }
    }


    /**
     * 判断是否是图片
     *
     * @param file
     */
    public static boolean isImage(File file) {
        String type = getMimeType(file);
        Pattern p = Pattern.compile("image/.*");
        Matcher m = p.matcher(type);
        return m.matches();
    }

    /**
     * 判断是否是pdf
     *
     * @param file
     */
    public static boolean isPdf(File file) {
        String type = getMimeType(file);
        return "application/pdf".equals(type);
    }

    /**
     * 判断是否是xls/xlsx
     *
     * @param file
     */
    public static boolean isXls(File file) {
        String type = getMimeType(file);
        return "application/vnd.ms-excel".equals(type);
    }

    /**
     * 判断是否是xls/xlsx
     *
     * @param file
     */
    public static boolean isXlsx(File file) {
        String type = getMimeType(file);
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(type);
    }


    /**
     * 判断是否是doc/docx
     *
     * @param file
     */
    public static boolean isWord(File file) {
        String type = getMimeType(file);
        return "application/msword".equals(type) || "application/vnd.openxmlformats-officedocument.wordprocessingml.document".equals(type);
    }
}